﻿
using Microsoft.EntityFrameworkCore;
using Models.Common;
using Models.Constant;
using Models.Device;
using Models.TaskPlain;
using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Models.Common.EnumDeviceState;
using GatewayServerService.CloudCenter;
using GatewayServerService.Data;
using static Models.Common.EnumDeviceType;
using UtilsORM;
using UtilsORM.MySqlData;

namespace GatewayServerService.Device
{
    public class ServiceDeviceGpio1001
    {
        EFYihongIotContext ef = new EFYihongIotContext();

        //回调获获继电器状态,为了避免误差，查状态和执行，都返回了 当前开关状态
        public void callBackGpioStatus(MQReqModel<object> httpReqModel)// (int status, string msg, CommandLogModel commandLogModel)
        {

            ModelReceverData modelDeviceData = httpReqModel.toServerData;
            CommandLogModel commandLogModel = httpReqModel.toGatewayData;

            if (httpReqModel.respCode == ResponseCode.SUCCESS && modelDeviceData != null)
            {
                string result = modelDeviceData.content;

                if (result == EnumGpioCommand.open || result == EnumGpioCommand.close)
                {
                    string name = "";
                    if (result == EnumGpioCommand.close)
                    {
                        name = "关闭";
                    }
                    else if (result == EnumGpioCommand.open)
                    {
                        name = "打开";
                    }
                    new ServiceCloudDeviceValueLog().writeSensorlog(commandLogModel.d_guid, "status", result, name);
                }
            }

        }
        public string sendCommandGpio(string dguid, string commandKey)
        {
            //状态，会在收到设备执行回调的时候，重新更新状态确认//dg_live_state='" + iot_Device_Gpio.dg_live_state + "',
            //string sql = "update iot_device set d_live_state='" + Enum.Parse(typeof(Gpio_Live_State), commandKey).GetHashCode() + "', d_live_state_set_time='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where d_guid='" + iot_Device.d_guid + "'";
            // MySqlYihongIot.ExecuteCommand(sql);

            iot_device device = new DBIotDeviceCache().getClientDeviceByCache(dguid);
            iot_device_command iot_Device_Command = new DBIotDeviceCommandCache().getCommandByCache(device.d_model_guid, commandKey);
            new ServiceCloudDeviceValueLog().writeSensorlog(dguid, "status", commandKey, iot_Device_Command.dc_name);


            //new ServiceCloudDeviceLiveState().setDeviceLiveState(dguid, Enum.Parse(typeof(Gpio_Live_State), commandKey).GetHashCode(), "");
            return new ServiceCloudDeviceCommand().sendCommandToGateWay(dguid, commandKey, "页面人工操作");
        }


        public int getStatus(string deviceGuid)
        {
            iot_device_live_state iot_Device = ef.iot_device_live_state.Where(c => c.ls_d_guid == deviceGuid).FirstOrDefault();
            if (iot_Device != null)
            {
                return iot_Device.ls_live_state;
            }
            return (int)Gpio_Live_State.error;
        }


    }
}
